package com.example.administrator.yuejian2.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.activity.SearchActivity;
import com.example.administrator.yuejian2.activity.Share_DetailActivity;
import com.example.administrator.yuejian2.adapter.ShareAdapter;
import com.example.administrator.yuejian2.bean.SharePictureBean;
import com.example.administrator.yuejian2.model.SharePicModel;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShareFragment extends Fragment {
    private RecyclerView recyclerView;
    private ShareAdapter shareAdapter;
    private SmartRefreshLayout smartRefreshLayout;
    private RetrofitListener<List<SharePictureBean>> listener;
    private List<SharePictureBean> sharelist;

    public ShareFragment() {

    }

    private void initView(View v) {
        recyclerView = v.findViewById(R.id.pic_recyclerview);
        smartRefreshLayout=v.findViewById(R.id.smartLayout);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        shareAdapter = new ShareAdapter(R.layout.share_item, sharelist);
        recyclerView.setAdapter(shareAdapter);
        shareAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        shareAdapter.addHeaderView(new Switch(getActivity()));
        shareAdapter.setHeaderViewAsFlow(true);
        shareAdapter.setFooterViewAsFlow(true);
        shareAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getActivity(), "item", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), Share_DetailActivity.class);
                intent.putExtra("s_id", sharelist.get(position).getPicture_id());
                getActivity().startActivity(intent);
            }
        });

        shareAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getActivity(),"time+1",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(),SearchActivity.class);
                getActivity().startActivity(intent);
            }
        });



    }
    private void SharepicList() {
        SharePicModel picModel = new SharePicModel();
        picModel.sharepicList(listener);
    }
    private void setPullRefresher(){
        //设置 Header 为 MaterialHeader
        smartRefreshLayout.setRefreshHeader(new MaterialHeader(getActivity()));
        //设置 Footer 为 经典样式
        smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()));

        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                shareAdapter.addData(sharelist);
                refreshlayout.finishRefresh(2000);
                //不传时间则立即停止刷新    传入false表示刷新失败
            }
        });
        smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {

                shareAdapter.addData(sharelist);

                refreshlayout.finishLoadmore(2000/*,false*/);//不传时间则立即停止刷新    传入false表示加载失败
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        listener = new RetrofitListener<List<SharePictureBean>>() {
            @Override
            public void onSuccess(final List<SharePictureBean> list, int flag) {
                sharelist = list;
                shareAdapter.addData(sharelist);
            }

            @Override
            public void onFail() {

            }
        };
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_share, null);
        initView(view);

        SharepicList();
        setPullRefresher();
        return view;
    }
}